package school.valon.rest.services;

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import school.valon.business.SurveyBusiness;
import school.valon.dto.SurveyAnswerDto;
import school.valon.dto.SurveyQuestionDto;

@Path("/surveys")
@PermitAll
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public class SurveyService {
	@Autowired()
	@Qualifier("surveyBusiness")
	private SurveyBusiness surveyBusiness;

	@POST
	@Path("/questions")
	@RolesAllowed("staff")
	@Produces(MediaType.APPLICATION_JSON)
	public Response createSurveyQuestion(SurveyQuestionDto surveyQuestionDto) throws Exception {
		surveyQuestionDto = surveyBusiness.saveSurveyQuestion(surveyQuestionDto);
		return Response.status(Response.Status.CREATED).entity(surveyQuestionDto).build();
	}

	@PUT
	@Path("/questions/{surveyQuestionId}")
	@RolesAllowed("staff")
	public Response updateSurveyQuestion(@PathParam("surveyQuestionId") Integer surveyQuestionId, SurveyQuestionDto surveyQuestionDto) throws Exception {
		surveyQuestionDto.setSurveyQuestionId(surveyQuestionId);
		surveyQuestionDto = surveyBusiness.saveSurveyQuestion(surveyQuestionDto);
		return Response.ok("Survey question updated").build();
	}

	@DELETE
	@Path("/questions/{surveyQuestionId}")
	@RolesAllowed("staff")
	public Response deleteSurveyQuestion(@PathParam("surveyQuestionId") Integer surveyQuestionId) throws Exception {
		surveyBusiness.deleteSurveyQuestion(surveyQuestionId);
		return Response.ok("Survey question deleted").build();
	}

	@POST
	@Path("/anwsers")
	@RolesAllowed({"user"})
	@Produces(MediaType.APPLICATION_JSON)
	public Response createSurveyAnswer(SurveyAnswerDto surveyAnswerDto) throws Exception {
		surveyAnswerDto = surveyBusiness.saveSurveyAnswer(surveyAnswerDto);
		return Response.status(Response.Status.CREATED).entity(surveyAnswerDto).build();
	}

	@PUT
	@Path("/anwsers/{surveyAnswerId}")
	@RolesAllowed({"user"})
	public Response updateSurveyAnswer(@PathParam("surveyAnswerId") Integer surveyAnswerId, SurveyAnswerDto surveyAnswerDto) throws Exception {
		surveyAnswerDto.setSurveyAnswerId(surveyAnswerId);
		surveyAnswerDto = surveyBusiness.saveSurveyAnswer(surveyAnswerDto);
		return Response.ok("Survey answer updated").build();
	}
}
